package org.csu.bbs.controller;

import org.csu.bbs.entity.Collection;
import org.csu.bbs.entity.Posts;
import org.csu.bbs.result.BetterResult;
import org.csu.bbs.result.Result;
import org.csu.bbs.service.CollectionService;
import org.csu.bbs.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CollectionController {
    @Autowired
    CollectionService collectionService;
    @Autowired
    PostsService postsServiceImpl;

    @CrossOrigin
    @PostMapping("api/collection")
    @ResponseBody
    public Result addCollection(@RequestBody Collection collection){
        return collectionService.addCollection(collection);
    }

    @CrossOrigin
    @GetMapping("api/user/{userid}/collections")
    @ResponseBody
    public List<Posts> ViewCollections(@PathVariable(value = "userid") String userid){
        return postsServiceImpl.GetCollection(userid);
    }

    @CrossOrigin
    @GetMapping("api/collectioned")
    @ResponseBody
    public boolean HadCollectioned(@RequestParam(value = "userid") String userid,@RequestParam(value = "postid") String postid){
        return collectionService.iscollectioned(userid, postid) != null;
    }

    @CrossOrigin
    @DeleteMapping("api/collection")
    @ResponseBody
    public Result quitCollection(@RequestParam(value = "userid") String userid,@RequestParam(value = "postid") String postid){
        return collectionService.deleteCollection(userid,postid);
    }

    @CrossOrigin
    @GetMapping("api/post/{id}/stars")
    @ResponseBody
    public BetterResult<Integer> GetPostStars(@PathVariable(value = "id") Integer pid){
        return collectionService.GetPostStars(pid);
    }
}
